<?php
//validator formulare pro obnoveni hesla - jestli ten hash existuje

require_once 'Zend/Validate/Abstract.php';

class Validate_HashExists extends Zend_Validate_Abstract {
    const INVALID = 'hashNotExists';

    /**
     * Validation failure message template definitions
     *
     * @var array
     */
    protected $_messageTemplates = array(
        self::INVALID => "Entered hash is no longer valid or exists.",
    );

    /**
     * Defined by Zend_Validate_Interface
     *
     * Returns true if and only if $value contains only hexadecimal digit characters
     *
     * @param  string $value
     * @return boolean
     */
    public function isValid($value) {
        $db = Zend_Db_Table::getDefaultAdapter();
        $select = $db->select()->from('forgot', new Zend_Db_Expr("COUNT(hash)"))->where("hash = ?", $value);
        $exists = (bool) $db->fetchOne($select);

        if (!$exists)
        {
             $this->_error(self::INVALID);
             return false;
        }

        return true;
    }

}
